/*
 *所有关于Aps_PostBindUser类的业务代码应在此处编写
*可使用repository.调用常用方法，获取EF/Dapper等信息
*如果需要事务请使用repository.DbContextBeginTransaction
*也可使用DBServerProvider.手动获取数据库相关信息
*用户信息、权限、角色等使用UserContext.Current操作
*Aps_PostBindUserService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
*/
using DGZImp.Core.BaseProvider;
using DGZImp.Core.Extensions.AutofacManager;
using DGZImp.ApsProject.DomainModels;
using System.Linq;
using DGZImp.Core.Utilities;
using System.Linq.Expressions;
using DGZImp.Core.Extensions;

using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using DGZImp.ApsProject.IRepositories;
using DGZImp.Core.EFDbContext;
using DGZImp.BasProject.DomainModels;
using DGZImp.ApsProject.Dtos;
using DGZImp.Core.Utilities.Response;
using System.Reflection.PortableExecutable;
using SqlSugar;

namespace DGZImp.ApsProject.Services
{
    public partial class Aps_PostBindUserService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly IAps_PostBindUserRepository _repository;//访问数据库
        public ServiceDbContext Db = AutofacContainerModule.GetService<ServiceDbContext>();

        [ActivatorUtilitiesConstructor]
        public Aps_PostBindUserService(
            IAps_PostBindUserRepository dbRepository,
            IHttpContextAccessor httpContextAccessor
            )
        : base(dbRepository)
        {
            _httpContextAccessor = httpContextAccessor;
            _repository = dbRepository;
            //多租户会用到这init代码，其他情况可以不用
            //base.Init(dbRepository);
        }

        public async Task<ApiResponse<List<PostUserWork>>> GetPostUserWork(ReqPostUserWork req)
        {



            var lst = await Db.Set<Aps_PostBindUser>()
                      .WhereIF(!string.IsNullOrEmpty(req.UserName), a => a.UserName.Contains(req.UserName))
                      .Select(a => new PostUserWork
                      {
                          UserName = a.UserName,
                          PostValue = a.PostValue,
                      }).ToListAsync();

            var posts = lst.Select(a => a.PostValue).Distinct().ToList();

            var machines = await Db.Set<Aps_PostBindMachine>().Where(a => posts.Contains(a.PostValue)).Select(a => new { a.MachineId, a.PostValue }).ToListAsync();


            var machineDate = await Db.Set<Aps_ScheduleTaskDate>()
                        .LeftJoin<Bas_Machine>((a, b) => a.MachineId == b.MachineId)
                        .Where(a => a.ScheduleVersionId == req.ScheduleVersionId)
                        .Where(a => a.StartTime <= req.EndTime && a.EndTime >= req.StartTime)
                        .Where(a => machines.Any(b => b.MachineId == a.MachineId))
                        .Select((a, b) => new ScheduleTaskDateAndMachine
                        {
                            ScheduleTaskDateId = a.ScheduleTaskDateId.SelectAll(),
                            MachineCodeName = b.MachineCode + " " + b.MachineName,
                        })
                        .ToListAsync();


            foreach (var item in lst)
            {

                item.MachineId = machines.Where(a => a.PostValue == item.PostValue).Select(a => a.MachineId).ToList();
                item.Childrens = machineDate.Where(a => item.MachineId.Any(b => b == a.MachineId)).ToList();
            }



            return ApiRes.Success(lst);
        }
    }
}
